package com.tss.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tss.pojo.Course;
import com.tss.pojo.Result;
import com.tss.pojo.Student;
import com.tss.service.ApplyService;
import com.tss.service.StudentService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("student")
public class StudentController {

	@Autowired
	private StudentService studentService;
	
	@Autowired
	private ApplyService applyService;
	
	@Autowired
	private HttpSession session;
	
	/**
	 * 根据身份证ID验证学员是否存在
	 * @param id
	 * @return
	 */
	@RequestMapping("check")
	@ResponseBody
	public String check(Student student){
		Result result = new Result();
		Student s = studentService.findStudent(student);
		if(s==null){
			result.setStatus(Result.STATUS_CODE_INEXISTENT);
			result.setMessage("学员不存在！");
		}else{
			result.setStatus(Result.STATUS_CODE_EXISTENT);
			result.setMessage("学员已经存在！");
		}
		
		return result.toString();
	}
	
	/**
	 * 查看学员个人信息
	 * @param id
	 * @return
	 */
	@RequestMapping("select")
	@ResponseBody
	public String select(Long id){
		Student student = new Student();
		student.setId(id);
		student=studentService.findStudent(student);
		return JSONObject.fromObject(student).toString();
	}
	
	/**
	 * 修改学员个人信息
	 * @param student
	 * @return
	 */
	@RequestMapping("update")
	@ResponseBody
	public String update(Student student){
		Result result = studentService.updateStudent(student);
		return result.toString();
	}
	
	/**
	 * 查看学员培训经历
	 * @param student
	 * @return
	 */
	@RequestMapping("experience")
	@ResponseBody
	public String experience(Student student){
		Result result = new Result();
		if(student==null||student.getId()==null){
			result.setErrorMessage("参数错误！");
			return result.toString();
		}
		Student s = studentService.findStudent(student);
		if(s==null){
			result.setErrorMessage("学员不存在！！");
			return result.toString();
		}
		
		List<Course> list = applyService.listCourse(student);
		
		if(list==null||list.isEmpty()){
			result.setErrorMessage("没有相关经历！");
			return result.toString();
		}
		result.setSuccessMessage("获取成功！");
		result.addResult("courses", list);
		return result.toString();
	}
	
	/**
	 * 获取学员列表
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="list")
	@ResponseBody
	public String list(@RequestParam(required=true,defaultValue="1") Integer pageNum,
            @RequestParam(required=false,defaultValue="10") Integer pageSize){
		List<Student> list = studentService.listStudent(null);
//		PageHelper.startPage(pageNum, pageSize);
//		PageInfo<Student> p = new PageInfo<>(list);
		return JSONArray.fromObject(list).toString();
	}
	
	@ResponseBody
	@RequestMapping("login")
	public String login(String idCardNum,String realName){
		Result result = new Result();
		if(idCardNum==null||realName==null){
			return result.setErrorMessage("参数错误！").toString();
		}
		Student student = new Student();
		student.setIdCardNum(idCardNum);
		Student s = studentService.findStudent(student);
		if(s==null){
			result.setErrorMessage("学员不存在！");
		}
		if(!s.getRealName().equals(realName)){
			return result.setErrorMessage("身份证与姓名不匹配！").toString();
		}
		session.setAttribute("user_id", s.getId());
		session.setAttribute("realName", s.getRealName());
		result.setSuccessMessage("登录成功！");
		return result.toString();
	}
	
	/**
	 * 查看课程安排
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("courselist")
	public String courseList(Long id){
		Student student = new Student();
		student.setId(id);
		List<Course> courses = applyService.listCourse(student);
		if(courses==null||courses.isEmpty()){
			return null;
		}
		return JSONArray.fromObject(courses).toString();
		
	}
	
	@ResponseBody
	@RequestMapping("register")
	public String register(Student student){
		Result result = studentService.addStudent(student);
		return result.toString();
	}
}
